//获取应用实例
import network from '../../utils/network.js';
var app = getApp();

Page({  
    data: {  
      login: false, 
      isuser:0,
      encry: [],
      userInfo:[],
    },  
    onShow:function(){    
        app.editTabBar();    //显示自定义的底部导航  
    },   

  onLoad: function () {
    // wx.showNavigationBarLoading() 
    // wx.showLoading({
    //   title: "数据加载中",
    // }) 
    // wx.hideNavigationBarLoading();
    var that = this; 
    wx.getStorage({
      key: 'user',
      success: function (res) { 
        if (res.data.is_user ==1){
          console.log(res.data.is_user);  
          wx.navigateTo({
            url: '/view/join/join'　　 
          })
        }else{ 
          that.setData({
            userInfo: res.data,
            login: true,
          });
        }
        
      },
      error:function(e){
        wx.login({
          success: function (re) {
            that.setData({
              code: re.code
            });
            that.onGetUsrCode("正在加载数据");
          }
        });
      } 
    }); 
   
   
  },
  // 获取删除
  onGetUsrCode: function (message) {
    var that = this;
    var data = {
      code: that.data.code,
    }; 
    network.requestLoading('http://jbc.com/login/Login/getCode', data, message, function (res) {
      if (res.code > 0) {
        wx.setStorage({
          key: 'user',
          data: res.data
        })
        that.setData({
          login:true,
          userInfo:res.data
        });
        //that.getSale_list('正在刷新数据')
      } else {
        // wx.showToast({
        //   title: res.msg,
        // });
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })

    })
  },
  /*
   *获取用户授权信息
   */
  onGotUserInfo() {
    var that = this;
    wx.login({
      success: function (re) { 
        that.setData({
          code: re.code
        });
        wx.getUserInfo({
          success: function (re) {
            that.setData({
              userInfo: {
                avatar: re.avatarUrl,
                nickName: re.nickName,
              },
              encry: re
            });
            that.onGetUserInfo("正在授权中");
          }
        });
      }
    });
    
   
  },

//授权新增用户
  onGetUserInfo: function (message) {
    var that = this;
    var encry = that.data.encry; 
    var code = that.data.code;
    var data = {
      code: that.data.code,
      code: code,
      signature: encry.signature,
      rawData: encry.rawData,
      encryptData: encry.encryptedData,
      iv: encry.iv
    }; 
    network.requestLoading('http://jbc.com/login/Login/getUserInfo', data, message, function (res) {
      if (res.code > 0) {
        console.log(res.data)
        that.setData({
          login: 1,
          userInfo: res.data,
        });
        //设置标题
        wx.setNavigationBarTitle({
          title: "会员中心"
        });
      } else {
        wx.showToast({
          title: res.msg,
        });
      }
    }, function (res) {
      wx.showToast({

        title: '加载数据失败',
      })

    })
  },

})

